﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KZ.Express.H.ENT;
using System.Data;
using KZ.Utilities;
using KZ.Express.H.DAL;

namespace KZ.Express.H.BLL
{
    public class PriceListBLL : BaseBLL, IBaseBLL
    {
        private new PriceListENT _entity;
        private ZoneDetailDAL _zoneDAL;

        public PriceListBLL()
            : base()
        {
            _zoneDAL = new ZoneDetailDAL();
            _entity = new PriceListENT();
        }


        public DataTable LoadSupplier(int carrierID)
        {
            SearchCriteria search = new SearchCriteria();
            search.Add("CarrierID", carrierID);
            search.Add("IsSupplier", 1);
            DataTable dt = _dal.GetDataTable<SupplierENT>(search);
            DataRow row = dt.NewRow();
            dt.Rows.InsertAt(row, 0);

            return dt;
        }

        public DataTable LoadCarrier()
        {
            DataTable dt = _dal.GetDataTable<CarrierENT>(new SearchCriteria());
            DataRow row = dt.NewRow();
            dt.Rows.InsertAt(row, 0);

            return dt;
        }

        public DataTable LoadZoneDetailType(int zoneID)
        {
            return _zoneDAL.SelectNumberOfType(zoneID);
        }



        public DataTable LoadPriceListDetail(int priceListID, string type)
        {
            SearchCriteria search = new SearchCriteria();
            search.Add("PriceListID", priceListID);
            search.Add("Type", type);
            DataTable dt = _dal.GetDataTable<PriceListDetailENT>(search);

            return dt;
        }

        /// <summary>
        /// original price
        /// </summary>
        /// <param name="priceList"></param>
        /// <param name="dox"></param>
        /// <param name="wpx"></param>
        /// <returns></returns>
        public int Save(PriceListENT priceList, DataTable dox, DataTable wpx)
        {
            try
            {
                base._dal.BeginTransaction();

                priceList.ValidateData();

                PriceListDetailDAL priceListDetailDAL = new PriceListDetailDAL();
                string action;
                Dictionary<string, object> content = new Dictionary<string, object>();
                SearchCriteria search = new SearchCriteria();
                if (priceList.PriceListID == 0)
                {
                    action = "Insert";
                    priceList.PriceListID = Utility.IntParse(_dal.Insert(priceList));

                    if (priceList.PriceListID == -1)
                        throw new BusinessLogicException("Mã bảng giá đã tồn tại, bạn vui lòng nhập mã bảng giá khác");
                    else if (priceList.PriceListID == -2)
                    {
                        priceList.IsRecovery = 1;
                        throw new BusinessLogicException("Dữ liệu có mã bảng giá này đã bị xóa, bạn có muốn khôi phục lại?");
                    }
                }
                else
                {
                    action = "Update";

                    search.Add("PriceListID", priceList.PriceListID);
                    content = _dal.ExecuteNonQueryOutParamDefineType(PriceListDetailENT.SelectContentProcedure, search, "Content", DbType.Xml.ToString());

                    base._dal.Update(priceList);
                    priceListDetailDAL.Delete(priceList.PriceListID);
                }

                int colCount = dox.Columns.Count;
                int j = 0;
                foreach (DataRow row in dox.Rows)
                {
                    PriceListDetailENT priceListDetailTemp = new PriceListDetailENT();
                    priceListDetailTemp.PriceListID = priceList.PriceListID;
                    priceListDetailTemp.Type = "DOX";
                    priceListDetailTemp.CreatedBy = UserENT._USERID;
                    priceListDetailTemp.UpdatedBy = UserENT._USERID;
                    //// convert weight then check weight is exact number or range
                    //decimal weight = Utility.DecimalParse(row["Weight"]);
                    //if (weight.HasValue) //// when weight is exact number, from weight = to weight
                    //    priceListDetailTemp.FromWeight = priceListDetailTemp.ToWeight = weight;
                    //else //// weight is range
                    //{
                    string[] temp = Utility.StringParse(row["Weight"]).Split('-');
                    if (temp.Length == 2)
                    {
                        priceListDetailTemp.FromWeight = Utility.DecimalParse(temp[0]);
                        priceListDetailTemp.ToWeight = Utility.DecimalParse(temp[1]);
                    }
                    else
                    {
                        decimal weight = Utility.DecimalParse(row["Weight"]);
                        priceListDetailTemp.FromWeight = weight;
                        priceListDetailTemp.ToWeight = weight;
                    }
                    //}

                    for (int i = 2; i < colCount; i++)
                    {
                        PriceListDetailENT priceListDetail = new PriceListDetailENT(priceListDetailTemp);
                        priceListDetail.Zone = dox.Columns[i].ColumnName;
                        priceListDetail.ListPrice = Utility.DecimalParse(row[i]);
                        priceListDetail.CreatedBy = UserENT._USERID;
                        priceListDetail.UpdatedBy = UserENT._USERID;

                        base._dal.Insert(priceListDetail);
                        priceList.PriceListDetails.Add(priceListDetail);

                    }
                    j++;
                }

                foreach (DataRow row in wpx.Rows)
                {
                    PriceListDetailENT priceListDetailTemp = new PriceListDetailENT();
                    priceListDetailTemp.PriceListID = priceList.PriceListID;
                    priceListDetailTemp.Type = "WPX";
                    priceListDetailTemp.CreatedBy = UserENT._USERID;
                    priceListDetailTemp.UpdatedBy = UserENT._USERID;
                    //// convert weight then check weight is exact number or range
                    //decimal weight = Utility.DecimalParse(row["Weight"]);
                    //if (weight.HasValue) //// when weight is exact number, from weight = to weight
                    //    priceListDetailTemp.FromWeight = priceListDetailTemp.ToWeight = weight;
                    //else //// weight is range
                    //{
                    string[] temp = Utility.StringParse(row["Weight"]).Split('-');
                    if (temp.Length == 2)
                    {
                        priceListDetailTemp.FromWeight = Utility.DecimalParse(temp[0]);
                        priceListDetailTemp.ToWeight = Utility.DecimalParse(temp[1]);
                    }
                    else
                    {
                        decimal weight = Utility.DecimalParse(row["Weight"]);
                        priceListDetailTemp.FromWeight = weight;
                        priceListDetailTemp.ToWeight = weight;
                    }
                    //}

                    for (int i = 2; i < colCount; i++)
                    {
                        PriceListDetailENT priceListDetail = new PriceListDetailENT(priceListDetailTemp);
                        priceListDetail.Zone = wpx.Columns[i].ColumnName;
                        priceListDetail.ListPrice = Utility.DecimalParse(row[i]);
                        priceListDetail.CreatedBy = UserENT._USERID;
                        priceListDetail.UpdatedBy = UserENT._USERID;
                        //if (priceListDetail.FromWeight.HasValue && priceListDetail.ListPrice.HasValue)
                        //{
                        base._dal.Insert(priceListDetail);
                        priceList.PriceListDetails.Add(priceListDetail);
                        //}
                    }
                    j++;
                }

                search.Clear();
                search.Add("PriceListID", priceList.PriceListID);
                search.Add("PriceListCode", priceList.PriceListCode);
                search.Add("Action", action);
                search.Add("UserID", priceList.CreatedBy);
                if (content.Count > 0)
                    search.Add("OldContent", content["Content"]);
                _dal.ExecuteNonQuery(PriceListDetailENT.InsertLogProcedure, search);

                base._dal.CommitTransaction();
                return priceList.PriceListID;
            }
            catch (DataAccessException ex)
            {
                base._dal.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }

        }
    }
}
